﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using MovieRental.BuisnessComponents.Base;
using System.Windows.Controls;
using System.Windows.Navigation;
using System.Collections.ObjectModel;

namespace MovieRental_Presentation.DataViews
{
    public abstract class BaseDataView : INotifyPropertyChanged
    {

        public event PropertyChangedEventHandler PropertyChanged;

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }

        protected EventHandler MakeEventHandlerForProperty(string propertyName)
        {
            return delegate(object sender, EventArgs args)
            {
                OnPropertyChanged(propertyName);
            };
        }

        protected void SetProperty<T>(ref T field, T value, string name)
        {
            if (!EqualityComparer<T>.Default.Equals(field, value))
            {
                field = value;
                OnPropertyChanged(name);
            }
        }

        protected void UpdateObservableCollection<T>(ObservableCollection<T> collection, IEnumerable<T> query)
        {
            collection.Clear();
            foreach (var item in query)
                collection.Add(item);
        }
    }
}
